package com.psnoted.backend.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;

/**
 * 用户数据实体类
 * 映射数据库user_data表
 * 
 * @author PSNoted Team
 * @version 1.0.0
 */
@Entity
@Table(name = "user_data", indexes = {
    @Index(name = "idx_user_data_key_hash", columnList = "key_hash"),
    @Index(name = "idx_user_data_timestamp", columnList = "timestamp"),
    @Index(name = "idx_user_data_version", columnList = "version")
})
public class UserData {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank(message = "密钥哈希不能为空")
    @Size(max = 255, message = "密钥哈希长度不能超过255个字符")
    @Column(name = "key_hash", nullable = false, length = 255)
    private String keyHash;

    @NotBlank(message = "数据版本不能为空")
    @Size(max = 20, message = "数据版本长度不能超过20个字符")
    @Column(name = "version", nullable = false, length = 20)
    private String version = "2.0";

    @NotBlank(message = "加密数据不能为空")
    @Column(name = "encrypted_data", nullable = false, columnDefinition = "TEXT")
    private String encryptedData;

    @NotNull(message = "时间戳不能为空")
    @Column(name = "timestamp", nullable = false)
    private Long timestamp;

    @CreationTimestamp
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    @UpdateTimestamp
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;

    // 关联用户
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "key_hash", referencedColumnName = "key_hash", insertable = false, updatable = false)
    private User user;

    // 构造函数
    public UserData() {}

    public UserData(String keyHash, String version, String encryptedData, Long timestamp) {
        this.keyHash = keyHash;
        this.version = version;
        this.encryptedData = encryptedData;
        this.timestamp = timestamp;
    }

    // Getter和Setter方法
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getKeyHash() {
        return keyHash;
    }

    public void setKeyHash(String keyHash) {
        this.keyHash = keyHash;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getEncryptedData() {
        return encryptedData;
    }

    public void setEncryptedData(String encryptedData) {
        this.encryptedData = encryptedData;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "UserData{" +
                "id=" + id +
                ", keyHash='" + keyHash + '\'' +
                ", version='" + version + '\'' +
                ", encryptedData='" + encryptedData + '\'' +
                ", timestamp=" + timestamp +
                ", createdAt=" + createdAt +
                ", updatedAt=" + updatedAt +
                '}';
    }
} 